﻿Ext.namespace('FantastPlatform', 'FantastPlatform.CaseManagement');
FantastPlatform.CaseManagement.AddPersonField = Ext.extend(Ext.form.TwinTriggerField, {
    initComponent: function () {
        FantastPlatform.CaseManagement.AddPersonField.superclass.initComponent.call(this);
    },
    onRender: function (ct, position) {
        FantastPlatform.CaseManagement.AddPersonField.superclass.onRender.call(this, ct, position);
        if (this.hiddenName) {
            this.hiddenField = this.el.insertSibling({ tag: 'input', type: 'hidden', name: this.hiddenName, id: (this.hiddenId || this.hiddenName) },
                                'before', true);
            this.hiddenField.value =
                            this.hiddenValue !== undefined ? this.hiddenValue :
                            this.value !== undefined ? this.value : '';      
        }
        var field = this;
        this.el.dom.ondblclick = function () { field.onTrigger2Click() };
    },
    setValue: function (v) {
        FantastPlatform.CaseManagement.AddPersonField.superclass.setValue.call(this, v);
        if (v !== undefined && v != '' && v) {
            var start = v.toString().indexOf('|');
            if (start != -1) {
                this.hiddenField.value = v.substring(0, start);
                this.setValue(v.substring(start + 1, v.length));
            }
        }
    },
    validationEvent: false,
    validateOnBlur: false,
    trigger1Class: 'x-form-clear-trigger',
    trigger2Class: 'x-form-search-trigger',
    // hideTrigger1: true,
    onTrigger1Click: function () {
        this.setValue('');
        if (this.grid) {
        } else {
            this.hiddenField.value = "";
        }
    },

    onTrigger2Click: function () {
        var id = GUID();        
        var items = [new FantastPlatform.UI.AddPersonForm({ url: this.url, Id: 0, onSClick: setV })];
        var field = this;
        function setV(name, value) {
            Ext.getCmp(id).close();
            if (field.grid) {
                field.grid.store.getAt(field.rowIndex).set('Value', name);

                field.grid.store.getAt(field.rowIndex).set('RealValue', value);
            } else {
                field.setValue(name);
                field.hiddenField.value = value;
            }

        };
        if (!this.disabled)
            openWindExt(id, '请选择' + this.fieldLabel, 570, 300, 'search', items, null);
    }
});